#include "abstractlistmodel.h"

AbstractListModel::AbstractListModel(QObject *parent)
    :QAbstractListModel(parent)
{

}

void AbstractListModel::addList(const AbstractList &list)
{
    beginInsertRows(QModelIndex(),rowCount(),rowCount());
    m_abstractList.append(list);
//    m_abstractList<<list;
    endInsertRows();
}

int AbstractListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_abstractList.count();
}

QVariant AbstractListModel::data(const QModelIndex &index, int role) const
{
  if(index.row()<0||index.row()>=m_abstractList.count())
      return QVariant();
  const AbstractList &abstractList=m_abstractList[index.row()];
  if(role==AbstractListRoles::NameRole)
      return abstractList.name();
  else if(role==AbstractListRoles::ColorRole)
      return abstractList.color();
  else if(role==AbstractListRoles::NumberRole)
          return abstractList.number();
  return QVariant();
}

QHash<int, QByteArray> AbstractListModel::roleNames() const
{
    QHash<int,QByteArray> roles;
    //use operator[]
//    roles[AbstractListRoles::NameRole]="name";            //定义对应角色值
//    roles[AbstractListRoles::ColorRole]="color1";
//    roles[AbstractListRoles::NumberRole]="number";
    //use insert
    roles.insert(AbstractListRoles::NameRole,"name");
    roles.insert(AbstractListRoles::ColorRole,"color1");
    roles.insert(AbstractListRoles::NumberRole,"number");
    return roles;
}

AbstractList::AbstractList(const QString &name, const QString &color, const QString &number)
    :m_name(name),m_color(color),m_number(number)
{

}

QString AbstractList::name() const
{
    return m_name;
}

QString AbstractList::color() const
{
    return m_color;
}

QString AbstractList::number() const
{
    return m_number;
}
